//
// Created by lymk on 2022/2/26.
//

#include "Buffer.h"
#include <vector>
#include <cstring>
unsigned char *Buffer::getData() const {
    return data;
}

void Buffer::setData(unsigned char *data) {
    Buffer::data = data;
}

int Buffer::getWidth() const {
    return width;
}

void Buffer::setWidth(int width) {
    Buffer::width = width;
}

int Buffer::getHeight() const {
    return height;
}

void Buffer::setHeight(int height) {
    Buffer::height = height;
}

int Buffer::getNChannels() const {
    return nChannels;
}

void Buffer::setNChannels(int nChannels) {
    Buffer::nChannels = nChannels;
}

Buffer::Buffer() {
    width = 0;
    height = 0;
    nChannels = 0;
    data = nullptr;
}

Buffer::Buffer(int w, int h, int c) {
    width = w;
    height = h;
    nChannels = c;
    data = new unsigned char[w*h*c];
}

std::vector<unsigned char> Buffer::GetElement(int i, int j) {
    if(i>=height || j>=width || i<=0 || j<=0)return {};
    std::vector<unsigned char> res;
    for(int k=(i*width+j)*nChannels;k<(i*width+j)*nChannels+nChannels;k++)
        res.push_back(data[k]);
    return res;
}

void Buffer::SetElement(int i, int j, std::vector<unsigned char> element) {
    if(i>=height || j>=width || i<=0 || j<=0)return;
    for(int k=0;k<nChannels;k++)
        data[(i*width+j)*nChannels+k] = element[k];
}

void Buffer::Clear() {
    memset(data,0,width*height*nChannels*sizeof(unsigned char));
}

float DepthBuffer::getDepth(int i, int j) {
    if(i>=height || j>=width || i<=0 || j<=0)return 0.f;
    return DepthValue[i*width+j];
}

void DepthBuffer::setDepth(int i, int j, float value) {
    if(i>=height || j>=width || i<=0 || j<=0)return ;
    DepthValue[i*width+j] = value;
}

DepthBuffer::DepthBuffer(int w,int h) {
    width = w;
    height = h;
    DepthValue = new float[w*h];
    for(int i=0;i<w*h;i++){
        DepthValue[i] = 100000.f;
    }
}

void DepthBuffer::Clear() {
    for(int i=0;i<width*height;i++){
        DepthValue[i] = 100000.f;
    }
}
